
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Call;
using Swaf.Container;
using Swaf.Security;

namespace UserLogin
{
	public class LocalToRemoteSecurityPrincipal : ISecurityPrincipal
	{
        protected static string s_cacheOption = "expire in 2 sec";
        protected static object s_emptyParams = new object[0];
        protected INameAddressSupport m_cacheNode;
        protected bool m_getting = false;

		public LocalToRemoteSecurityPrincipal()
		{
            m_cacheNode = Application.globalApp.factories["GeneralContainerNodes"].createFromPattern(s_cacheOption, s_emptyParams) as INameAddressSupport;
		}

        public ISecurityPrincipal Principal
        {
            get
            {
                ISecurityPrincipal p = m_cacheNode.get(null,s_emptyParams) as ISecurityPrincipal;
                if (p == null)
                {
                    m_getting = true;
                    try
                    {
                        IResults rc = Application.currentApp.ruleMgr.execute("getRemoteSecurityPrincipal");
                        m_cacheNode.put(null, p = rc.CallResults as ISecurityPrincipal);
                    }
                    finally
                    {
                        m_getting = false;
                    }
                }
                //TODO: Handle exception
                return p;
            }
        }
		#region ISecurityPrincipal Members

		public INameAddressSupport additionalInfo
		{
			get { return m_getting?null:Principal.additionalInfo; }
		}

		public void assert(string resourceName, Privileges minPrivilege)
		{
			Principal.assert(resourceName, minPrivilege);
		}

		public Privileges getPrivilege(string resourceName)
		{
			return Principal.getPrivilege(resourceName);
		}
		public Privileges getPrivilege(string resourceName, bool x)
		{
			return Principal.getPrivilege(resourceName, x);
		}

		public bool hasPrivilege(string resourceName)
		{
			return Principal.hasPrivilege(resourceName);
		}

		public string instanceId
		{
			get { return m_getting?"":Principal.instanceId; }
		}

		public bool isUserInRole(string roleName)
		{
			return Principal.isUserInRole(roleName);
		}

		public INameAddressSupport resourceSets
		{
			get { return Principal.resourceSets; }
		}

		public INameAddressSupport roles
		{
			get { return Principal.roles; }
		}

		public ICall rolesCall
		{
			get { return Principal.rolesCall; }
		}

		public string userName
		{
			get { return m_getting?null:Principal.userName; }
		}

		public bool deepCloneResourceSets
		{
			get
			{
				throw new Exception("The method or operation is not implemented.");
			}
			set
			{
				throw new Exception("The method or operation is not implemented.");
			}
		}

		#endregion

		#region INameAddressSupport Members

		public XmlSerializationSupport SerializationSupport
		{
			get { return Principal.SerializationSupport; }
		}

		public string Xml
		{
			get { return Principal.Xml; }
		}

		public bool containsField(string nameAddress)
		{
			return Principal.containsField(nameAddress);
		}

		public object get(string nameAddress)
		{
			return Principal.get(nameAddress);
		}

		public object get(string nameAddress, object oneArg)
		{
			return Principal.get(nameAddress, oneArg);
		}

		public object get(string nameAddress, params object[] infoArgs)
		{
			return Principal.get(nameAddress, infoArgs);
		}

		public string getXml(string options)
		{
			return Principal.getXml(options);
		}

		public bool isGetOnly
		{
			get { return Principal.isGetOnly; }
		}

		public void put(string nameAddress, object val, object extraInfo)
		{
			Principal.put(nameAddress, val, extraInfo);
		}

		public void put(string nameAddress, object val)
		{
			Principal.put(nameAddress, val);
		}

		#endregion
		public System.Collections.IEnumerator GetEnumerator() { return null; }

	}
}
